﻿using System;
using Glue;
using Glue.Internals;
using GlueTests.Builders;
using Xunit;

namespace GlueTests.RelationContainerTests
{
    public class When_adding_a_relation_two_times
    {
        private readonly Mapping<DomainPerson, GuiPerson> mapping;
        private RelationContainer<DomainPerson, GuiPerson> relations;

        public class DomainPerson { public String Name { get; set; } }
        public class GuiPerson { public String Name { get; set; } }

        public When_adding_a_relation_two_times()
        {
            mapping = new Mapping<DomainPerson, GuiPerson>();
            relations = mapping.Relations;
            mapping.Relate(domain=>domain.Name,gui=>gui.Name);
            mapping.RelateTowardsRight(domain => domain.Name, gui => gui.Name);
        }

        [Fact]
        public void Should_only_be_one_relation()
        {
            Assert.Equal(1,relations.Count);
        }

        [Fact]
        public void The_last_added_should_override_previously_added()
        {
            Assert.Equal(1,relations.GetRelationsTowardsRight().Count);
            Assert.Equal(0, relations.GetRelationsTowardsLeft().Count);
        }

        [Fact]
        public void Should_still_be_related_towards_right_as_the_last_added_is()
        {
            Assert.True(
                relations.IsRelatedTowardsOnRightSide(new PropertyBuilder(typeof(DomainPerson),"Name").Build()));
        }

        [Fact]
        public void Should_not_be_related_towards_left_as_the_last_added_isnt()
        {
            Assert.False(
                relations.IsRelatedTowardsOnLeftSide(new PropertyBuilder(typeof(DomainPerson), "Name").Build()));
        }
    }
}
